package com.company.app.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import com.luciad.imageio.webp.WebPReadParam;
import com.luciad.imageio.webp.WebPWriteParam;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.ImageWriter;
import javax.imageio.IIOImage;

public class WebPCase {
  public static final String PNG_EXTENSION = "png";
  public static final String DOT_PNG_EXTENSION = ".png";
  public static final String WEBP_EXTENSION = "webp";
  public static final String DOT_WEBP_EXTENSION = ".webp";
  public static final String FILE_TYPE_WEBP = "image/webp";

  public void testWebPCase() {
    String wepFileUrl = "C:\\Users\\user\\Pictures\\15321-9.webp";
    String pngFileUrl = "C:\\Users\\user\\Pictures\\0-15321-9.png";

    // 单张指定文件转换
    webp2png(wepFileUrl, pngFileUrl);
  }

  /**
   * * 批量将文件夹跟目录webp格式图片转成png格式
   * * @param
   * * @return
   */
  public void batchFileWebp2Png(String folderPath) {
    File folderFileObj = new File(folderPath);
    File[] webpFiles = folderFileObj.listFiles();
    if (webpFiles != null) {
      for (File webpFile : webpFiles) {
        if (webpFile.isFile() && webpFile.getName().endsWith(DOT_WEBP_EXTENSION)) {
          System.out.println(webpFile.getName());
          String webpFilePath = webpFile.getAbsolutePath();
          String pngFilePath = webpFile.getAbsolutePath().replace(DOT_WEBP_EXTENSION, DOT_PNG_EXTENSION);
          webp2png(webpFilePath, pngFilePath);
        }
      }
    }
  }

  /**
   * webp格式图片转成png格式
   * 
   * @param wepFile
   * @param pngFile
   */
  public void webp2png(String webpFile, String pngFile) {
    // 创建WebP ImageReader实例
    ImageReader reader = ImageIO.getImageReadersByMIMEType(FILE_TYPE_WEBP).next();
    // 配置解码参数
    WebPReadParam readParam = new WebPReadParam();
    readParam.setBypassFiltering(true);
    // 在ImageReader设置读取的原文件
    try {
      reader.setInput(new FileImageInputStream(new File(webpFile)));
      // 解码图像
      BufferedImage image = reader.read(0, readParam);
      // 设置输入文件的格式和文件名
      ImageIO.write(image, PNG_EXTENSION, new File(pngFile));
      // 这里也可以使用其他图片格式，但是格式和文件名后缀要保持一致
      System.out.println("Successfully converted webp file to png format\n");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * PNG格式图片转成webp格式(也可以是其他格式图片)
   */
  public void png2webp(String pngFile, String webpFile) {
    try {
      // 获取原始文件的编码
      BufferedImage image = ImageIO.read(new File(pngFile));
      // 创建WebP ImageWriter实例
      ImageWriter writer = ImageIO.getImageWritersByMIMEType(FILE_TYPE_WEBP).next();
      // 配置编码参数
      WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
      // 设置压缩模式
      writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT);
      // 配置ImageWriter输出
      writer.setOutput(new FileImageOutputStream(new File(webpFile)));
      // 进行编码，重新生成新图片
      writer.write(null, new IIOImage(image, null, null), writeParam);
      System.out.println("successfullt convert png file to webp format\n");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
